/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.eclipse.create_toolbox_project.ui;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;

/**
 * Class representing the first page of the wizard.
 */
public class NewToolboxProjectMainPage extends WizardPage implements
		Listener {

	/**
	 * Text input for project name.
	 */
	private Text projectName;

	/**
	 * Text input for project location.
	 */
	private Text projectLocation;
	
	/**
	 * Button to browse the directory hierarchy to choose the project location.
	 */
	private Button browse;
	
	/**
	 * Directory dialog to select a project location.
	 */
	private DirectoryDialog chooseProjectLocation;

	/**
	 * Constructor for NewToolboxProjectMainPage.
	 * 
	 * @param workbench to use
	 * @param selection to use
	 */
	NewToolboxProjectMainPage(IWorkbench workbench,
			IStructuredSelection selection) {
		super(NewToolboxProjectMainPage.class.getName());
		setTitle("New Toolbox project");
		setDescription("Set details of project");

	}

	/**
	 * Method to create controls. We display ui components on parent.
	 * 
	 * @param parent on what to add components.
	 */
	public void createControl(Composite parent) {

		// create the composite to hold the widgets
		GridData gd;
		Composite composite = new Composite(parent, SWT.NULL);

		// create the desired layout for this wizard page
		GridLayout gl = new GridLayout();
		int ncol = 4;
		gl.numColumns = ncol;
		composite.setLayout(gl);

		new Label(composite, SWT.NONE).setText("Name:");
		// text for project name
		setProjectName(new Text(composite, SWT.BORDER));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = ncol - 1;
		getProjectName().setLayoutData(gd);

		createLine(composite, ncol);

		new Label(composite, SWT.NONE).setText("Location:");
		// text for project location
		gd = new GridData(GridData.FILL_HORIZONTAL);
		setProjectLocation(new Text(composite, SWT.BORDER));
		getProjectLocation().setEditable(false);
		getProjectLocation().setLayoutData(gd);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 1;
		// button to display directory dialog
		setBrowse(new Button(composite, SWT.BORDER));
		getBrowse().setText("browse");
		getBrowse().setLayoutData(gd);

		// directory dialog to choose project location
		setChooseProjectLocation((new DirectoryDialog(
				parent.getShell())));
		getChooseProjectLocation().setMessage("message");

		setControl(composite);
		addListeners();
		
	}

	/**
	 * Add all listerners on components.
	 */
	private void addListeners()
	{
		
		getProjectName().addListener(SWT.KeyUp, this);
		getBrowse().addListener(SWT.Selection, this);
		
	}

	/**
	 * Handle event on all listeners on components.
	 * 
	 * @param event which must be handled
	 */
	public void handleEvent(Event event) {

	    if ((event.widget == getBrowse())) {

	    	String location = (String) chooseProjectLocation.open();
	    	
	    	projectLocation.setText(location);
			
	    }

		getWizard().getContainer().updateButtons();
	}


	/**
	 * Helper method to check is value of org.eclipse.swt.widgets.Text is empty.
	 * 
	 * @param text to check
	 * @return true if text is not null and text inside is not null and not empty
	 */
	private static boolean isTextNonEmpty(Text text) {
		if (text != null) {
			String s = text.getText();
			if ((s != null) && (s.trim().length() > 0)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Create empty line on page.
	 * 
	 * @param parent to add line
	 * @param ncol to put on line
	 */
	private void createLine(Composite parent, int ncol) {
		Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = ncol;
		line.setLayoutData(gridData);
	}

	/**
	 * Indicates if wizard can finish. Project name and location must be set.
	 * 
	 * @return true if wizard can finish.
	 */
	boolean canFinish() {
		return isTextNonEmpty(projectName) && isTextNonEmpty(projectLocation);
	}

	/**
	 * getter for the projectName property.
	 * @return the projectName
	 */
	Text getProjectName() {
		return projectName;
	}

	/**
	 * setter for the projectName property.
	 * @param projectName the projectName to set
	 */
	void setProjectName(Text projectName) {
		this.projectName = projectName;
	}

	/**
	 * getter for the projectLocation property.
	 * @return the projectLocation
	 */
	Text getProjectLocation() {
		return projectLocation;
	}

	/**
	 * setter for the projectLocation property.
	 * @param projectLocation the projectLocation to set
	 */
	void setProjectLocation(Text projectLocation) {
		this.projectLocation = projectLocation;
	}

	/**
	 * getter for the browse property.
	 * @return the browse
	 */
	Button getBrowse() {
		return browse;
	}

	/**
	 * setter for the browse property.
	 * @param browse the browse to set
	 */
	void setBrowse(Button browse) {
		this.browse = browse;
	}

	/**
	 * getter for the chooseProjectLocation property.
	 * @return the chooseProjectLocation
	 */
	DirectoryDialog getChooseProjectLocation() {
		return chooseProjectLocation;
	}

	/**
	 * setter for the chooseProjectLocation property.
	 * @param chooseProjectLocation the chooseProjectLocation to set
	 */
	void setChooseProjectLocation(DirectoryDialog chooseProjectLocation) {
		this.chooseProjectLocation = chooseProjectLocation;
	}

	

}
